package com.lan.workflow.common;

import com.baomidou.mybatisplus.annotation.TableName;
import com.lan.workflow.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class TableNameEntityClassMapping {
    private static Logger logger = LoggerFactory.getLogger(TableNameEntityClassMapping.class);

    public static Map<String, String> mapping = new HashMap<>();

    public TableNameEntityClassMapping() {
        try {
            Scanner scanner = new Scanner();
            Set<Class<?>> classes = scanner.getClasses("com.lan.workflow.entity");
            if (classes != null && classes.size() > 0) {
                for (Class<?> cls : classes) {
                    TableName annotation = cls.getAnnotation(TableName.class);
                    if (annotation != null) {
                        mapping.put(annotation.value(), cls.getName());
                    }
                }
            }
        } catch (Exception e) {
            logger.error("EntityClassTableNameMapping() ", e);
        }
    }

    @PreDestroy
    public void destroy(){
        mapping.clear();
    }
}
